package com.neusoft.weixin.web.account;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.neusoft.common.util.WeixinConfigUtil;
import com.neusoft.weixin.service.account.MenuService;


/**
 * 菜单管理
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Autowired
	public MenuService menuService;
	
	private Logger log = LoggerFactory.getLogger(MenuController.class);
	
	/**
	 * 获取菜单信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getTreeData") 
	public String getTreeData(HttpServletRequest request, HttpServletResponse response){
		PrintWriter out = null;
		try {
			out = response.getWriter();
			JSONArray result = menuService.getTreeData();
    		out.write(result.toString());
		} catch (IOException e) {
			log.error(e.getMessage());
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		return null;
	}
	
	@RequestMapping(params = "method=addOrEditMenu") 
	public String addOrEditMenu(HttpServletRequest request, HttpServletResponse response){
		PrintWriter out = null;
		JSONObject result = null;
		try {
			out = response.getWriter();
			JSONObject data = JSONObject.fromObject(request.getParameter("data"));
			result = menuService.addOrEditMenu(data);
    		out.write(result.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
			if(out!=null){
				result = new JSONObject();
				result.put("status", "-1");
				result.put("detail", "操作出现异常!");
				out.write(result.toString());
			}
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		return null;
	}
	
	/**
	 * 删除菜单
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=deleteMenu")
	public String deleteMenu(HttpServletRequest request, HttpServletResponse response){
		PrintWriter out = null;
		JSONObject result = null;
		try {
			out = response.getWriter();
			JSONObject data = JSONObject.fromObject(request.getParameter("data"));
			result = menuService.deleteMenu(data.getString("MENU_ID"));
    		out.write(result.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
			if(out!=null){
				result = new JSONObject();
				result.put("status", "-1");
				result.put("detail", "删除菜单失败!");
				out.write(result.toString());
			}
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		return null;
	}
	
	
	/**
	 * 创建自定义菜单
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=createCustomMenu")
	public String createCustomMenu(HttpServletRequest request, HttpServletResponse response){
		
		log.info("创建服务号自定义菜单");
		PrintWriter out = null;
		JSONObject result = null;
		try {
			out = response.getWriter();
			result = menuService.createCustomMenu();
    		out.write(result.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
			if(out!=null){
				result = new JSONObject();
				result.put("status", "-1");
				result.put("detail", "创建菜单失败!");
				out.write(result.toString());
			}
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		return null;
	}
	
	@RequestMapping(params = "method=getConfig")
	public String getConfig(HttpServletRequest request, HttpServletResponse response){
		
		log.info("获取服务号配置");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			JSONObject data = JSONObject.fromObject(WeixinConfigUtil.getPropertys());
    		out.write(data.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		
		return null;
	}
		
		
		
	
	@RequestMapping(params = "method=saveConfig")
	public String saveConfig(HttpServletRequest request, HttpServletResponse response){
		
		
		log.info("保存服务号配置");
		
		PrintWriter out = null;
		JSONObject respJSON = null;
		try {
			out = response.getWriter();
			JSONObject data = JSONObject.fromObject(request.getParameter("data"));
			Boolean result = WeixinConfigUtil.putPropertys(data);
			respJSON = new JSONObject();
			if ( result ){
				respJSON.put("status", "1");
				respJSON.put("detail", "保存成功！");
			}else{
				respJSON.put("status", "-1");
				respJSON.put("detail", "保存失败！");
			}
    		out.write(respJSON.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
			if(out!=null){
				respJSON = new JSONObject();
				respJSON.put("status", "-1");
				respJSON.put("detail", "删除菜单失败!");
				out.write(respJSON.toString());
			}
		}finally{
			if(out!=null){
				out.flush();
				out.close();
			}
		}
		
		return null;
		
	}
		

}
